package org.huawei260.basic.part_3_tree.Heap;

/**
 * @Author: qingle
 * @Date: 2024/11/29-16:09
 * @Description: 实现分块读取、去重和排序的逻辑。
 * @Version: 1.0
 */
import java.io.*;
import java.util.*;

public class FileProcessor {
	private static final int BUFFER_SIZE = 100_000; // 每块处理1,000,000个ID
	private static final int MAX_VALUE = 100_000_000; // 假设ID范围是0到99,999,999

	public static void main(String[] args) throws IOException {
		File file = new File("ids.txt");
		try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
			Set<Integer> uniqueIds = new TreeSet<>(); // 使用TreeSet进行去重和排序
			String line;
			int count = 0;

			while ((line = reader.readLine()) != null) {
				uniqueIds.add(Integer.parseInt(line.split(":", 2)[1]));
				count++;

				// 当达到BUFFER_SIZE或文件末尾时，写入临时文件
				if (count % BUFFER_SIZE == 0) {
					writeTempFile(uniqueIds);
					uniqueIds.clear();
				}
			}

			// 处理剩余的ID
			if (!uniqueIds.isEmpty()) {
				writeTempFile(uniqueIds);
			}
		}
	}

	private static void writeTempFile(Set<Integer> uniqueIds) throws IOException {
		int tempFileIndex = uniqueIds.size() / BUFFER_SIZE;
		File tempFile = new File("temp" + tempFileIndex + ".txt");
		try (BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile))) {
			for (Integer id : uniqueIds) {
				writer.write(id.toString());
				writer.newLine();
			}
		}
	}
}